package com.dw.etl.core.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dw.etl.core.bean.vo.Msg;
import com.dw.etl.core.bean.model.*;
import com.dw.etl.core.mapper.*;
import com.dw.etl.core.api.CoreAuthorizationService;
import com.dw.etl.core.utils.EmailUtil;
import com.dw.etl.core.utils.Md5Util;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author GuoPeng
 * @Title: CoreAuthorizationServiceImpl
 * @Description: 权限核心业务
 * @date 2019/5/8 10:54
 */
@Service
@Transactional
public class CoreAuthorizationServiceImpl implements CoreAuthorizationService {


    @Resource
    private SysUsersMapper sysUsersMapper;

    @Resource
    private SysUsersRolesMapper sysUsersRolesMapper;

    @Resource
    private SysRolesPermissionsMapper sysRolesPermissionsMapper;

    @Resource
    private SysTagMapper sysTagMapper;

    @Resource
    private SysTagPermissionsMapper sysTagPermissionsMapper;


    //登录的用户信息
    private static   Map LoginToken=new HashMap();
    //邮箱  登录  一个邮箱只能注册一次
    private  static  Map yzmInfo=new HashMap();

    @Override
    public Msg authorization(String username, String password,String yzm) {
        //用户名 获取盐值
        HashMap parm = new HashMap();
        parm.put("username", username);
        SysUsers sysUsers = sysUsersMapper.selectByusernameAndpassword(parm);
        if(sysUsers==null){
            return Msg.fail().add("error", "没有该用户");
        }
        String md5password = Md5Util.getMD5WithSalt(password, sysUsers.getSalt());
        //密码加密获取 用户信息
        parm.put("password", md5password);
        SysUsers User = sysUsersMapper.selectByusernameAndpassword(parm);
        if(User==null){
            return Msg.fail().add("error", "密码错误");
        }

        //获取用户的角色信息
        parm.put("user_id", User.getId());
        List<SysTag> sysTags = new ArrayList<>();
        List<SysUsersRoles> sysUsersRoles = sysUsersRolesMapper.selectbyUseridAndRoleid(parm);
        for (SysUsersRoles sysUsersRole : sysUsersRoles) {
            parm.put("role_id", sysUsersRole.getRoleId());
            List<SysRolesPermissions> sysRolesPermissions = sysRolesPermissionsMapper.selectByRolesidAndPermissionsid(parm);
            for (SysRolesPermissions sysRolesPermission : sysRolesPermissions) {
                HashMap pram = new HashMap();
                pram.put("permissions_id", sysRolesPermission.getPermissionId());
                List<SysTagPermissions> sysTagPermissions = sysTagPermissionsMapper.selectbypridAndtagId(pram);
                for (SysTagPermissions sysTagPermission : sysTagPermissions) {
                    SysTag sysTag = sysTagMapper.selectByPrimaryKey(sysTagPermission.getTagId());
                    sysTags.add(sysTag);
                }

            }
        }
        LoginToken.put("LoginToken"+User.getId(), UUID.randomUUID().toString().replace("-","" ));
        HashMap returnMap = new HashMap();
        returnMap.put("sysTags", sysTags);
        returnMap.put("user", User);
        returnMap.put("LoginToken", LoginToken.get("LoginToken"+User.getId()));
        return Msg.success().add("LoginInfo", returnMap);
    }

    @Override
    public Msg getEmailyzm(String email) {
            String yzm = UUID.randomUUID().toString().replace("-", "").substring(0, 5);
            EmailUtil.send_mail(email, yzm, "SharreTieba 验证码：");
            yzmInfo.put(email, yzm);
            return Msg.success().add("yzm", yzm);
    }

    @Override
    public Msg autLoginToken(String LoninToken) {
        Map loginToken = LoginToken;
        if(loginToken.size()==0){
            return  Msg.fail();
        }
        for (Object key : loginToken.keySet()) {
            if(LoninToken.equals(loginToken.get(key))){
                return  Msg.success();
            }
        }
        return  Msg.fail();
    }

    @Override
    public Boolean autLoginToken_sta(String LoninToken) {
        Map loginToken = LoginToken;
        if(loginToken.size()==0){
            return  false;
        }
        for (Object key : loginToken.keySet()) {
            if(LoninToken.equals(loginToken.get(key))){
                return  true;
            }
        }
        return  false;
    }

}
